﻿using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// 用户数据代理
/// </summary>
public class UserProxy
{
    public UserProxy()
    {
    }

    /// <summary>
    /// 添加用户
    /// </summary>
    /// <param name="account"></param>
    /// <param name="code"></param>
    /// <param name="access"></param>
    /// <returns>如果添加成功，返回添加的用户信息；失败则返回错误原因</returns>
    public object AddUser(string account, string code, int access)
    {
        DBUser db = new DBUser();
        if (db.SelectUser(account) != null)
            return "帐号已存在";
        db.InsertUser(new UserData(account, code, access));
        return db.SelectUser(account);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public object DeleteUser(int id)
    {
        DBUser db = new DBUser();
        UserData result = db.SelectUser(id);
        if (result != null)
        {
            db.DeleteUser(id);
            db.DeleteBlogAccountMapByUserId(id);
        }
        return result;
    }

    public object SelectUser(int id)
    {
        DBUser db = new DBUser();
        UserData result = db.SelectUser(id);
        if (result != null)
        {
            result.blogAccountList = db.SelectBlogAccountMap(result.id);
            return result;
        }
        else
            return "帐号不存在";
    }

    public  object SelectUser(string account)
    {
        DBUser db = new DBUser();
        UserData result = db.SelectUser(account);
        if (result != null)
        {
            result.blogAccountList = db.SelectBlogAccountMap(result.id);
            return result;
        }
        else
            return "帐号不存在";
    }

    public object UpdateUser(int id, string account, string password, int access)
    {
        DBUser db = new DBUser();
        UserData user = db.SelectUser(id);
        if (user != null)
        {
            if (account != null)
                user.name = account;
            if (password != null)
                user.password = password;
            if (access != 0)
                user.access = access;
            db.UpdateUser(user);
        }
        return user;
    }

    public List<UserData> SelectAllUser()
    {
        DBUser db = new DBUser();
        List<UserData> result = db.SelectAllUser();
        foreach (UserData user in result)
        {
            user.blogAccountList = db.SelectBlogAccountMap(user.id);
        }
        return result;
    }
}